app.directive("smallhead",function(){
	return {
		templateUrl:"commen/head.html",
		css:"commen/head.css",
		replace:true,
		restrict:"E"
	}
});
app.controller("cartCtrl",["$scope","$http","sc","$rootScope",function($scope,$http,sc,$rootScope){
	$scope.buy=sc.data;
	/*$scope.carData=JSON.parse(window.localStorage.car)*/
	$rootScope.flag=true;
	$scope.addGOOds=function(i,num){
			if(i.num){
				i.num+=num;
			}else{
				i.num=1;
			}
			/*if(window.localStorage.car){
				obj=JSON.parse(window.localStorage.car);
			}
			obj[i.id]=i;
			window.localStorage.setItem("car",JSON.stringify(obj));*/
			/*window.localStorage.setItem("numm",JSON.stringify(i.num));
			i.num=window.localStorage.numm;*/
			sc.addGs(i);
			if(i.num==0){
				sc.delGs(i);
			}
			$rootScope.num1+=num;
			if(i.state==true){
			$scope.doller+=num*(i.price);
			}
	
	}
	$scope.doller=sc.money($scope.buy);
	$scope.state=true;
	$scope.selectAll=function(){
			sc.selectAll($scope.state);
			if($scope.state==true){
			$scope.doller=sc.money($scope.buy);
			}else{
				$scope.doller=0;
			}

	}
	$scope.selectFn=function(i){
		$scope.state=sc.selectFn($scope.state);
		if(i.state==true){
		$scope.doller+=(i.num)*(i.price);
		}else{
		$scope.doller-=(i.num)*(i.price);			
		}
	}
	}
])
app.controller("homeCtrl",["$scope","$http","$q","$timeout","sc","$rootScope",function($scope,$http,$q,$timeout,sc,$rootScope){
	$rootScope.flag=true;
	$http({
		url:'http://h5.yztctech.net/api/axf/apihome.php',
		method:"GET"
		
	}).success(function(res){
			$scope.dataList=res.data.slide;
			$scope.dataList1=res.data.menu; 
			$timeout(function(){
				var mySwiper = new Swiper ('.swiper-container', {
   	autoplay: 1000,
   	touchRatio : 0.5,
   	autoplayDisableOnInteraction : false,
   	loop:true,

    // 如果需要分页器
    pagination: '.swiper-pagination'
    
    // 如果需要前进后退按钮
   // nextButton: '.swiper-button-next',
    //prevButton: '.swiper-button-prev',
    
    // 如果需要滚动条
    //scrollbar: '.swiper-scrollbar',
  }) }); 
	});
	$http({
		url:'http://h5.yztctech.net/api/axf/apihomehot.php',
		method:"GET"
	}).success(function(res){
			$scope.hotDate=res.data;
	})
	$scope.addGoods=function(i){
		if(i.num){
				i.num++;
			}else{
				i.num=1;
			}
			i.state=true;
			$rootScope.num1++;
			sc.addGs(i);
	}
	}
])
app.controller("marketCtrl",["$scope","$http","sc","$rootScope",function($scope,$http,sc,$rootScope){
		$rootScope.flag=true;
			$scope.Odate=data1.data;
			$scope.date=data1.data.categories;
			var firstId=$scope.date[0].id;
			$scope.currentgood=$scope.Odate.products[firstId];
			/*$scope.currentgood.map(function(item){
					item.count=0;
			})*/
		
		$scope.chooseMenu=function(i,text,m){
			$scope.index=m;
		var goodId=i.id;
		$scope.currentgood=$scope.Odate.products[goodId];
		/*$scope.currentgood.map(function(item){
					item.count=0;
			})*/
		}
		var obj={};
		$scope.addGOODs=function(i,num){
			if(i.num){
				i.num+=num;
			}else{
				i.num=1;
			}
			i.state=true;
			$rootScope.num1+=num;
			/*if(window.localStorage.car){
				obj=JSON.parse(window.localStorage.car);
			}
			obj[i.id]=i;
			window.localStorage.setItem("car",JSON.stringify(obj));*/
			/*window.localStorage.setItem("numm",JSON.stringify(i.num));
			i.num=window.localStorage.numm;*/
			sc.addGs(i);
			if(i.num==0){
				sc.delGs(i);
			}
	}
	}
])
app.controller("mineCtrl", ["$scope", "$http","$rootScope", function($scope, $http,$rootScope) {
	$rootScope.flag=true;
	$scope.data = [{
		url: "template/mine/imgs/1.png",
		text: "待付款"
	}, {
		url: "template/mine/imgs/2.png",
		text: "待收货"
	}, {
		url: "template/mine/imgs/3.png",
		text: "待评价"
	}, {
		url: "template/mine/imgs/4.png",
		text: "退款/售后"
	}];
	$scope.qtdata = [{
		url: "template/mine/imgs/5.png",
		text: "积分商城"
	}, {
		url: "template/mine/imgs/6.png",
		text: "优惠券"
	}, {
		url: "template/mine/imgs/7.png",
		text: "收货地址"
	}, {
		url: "template/mine/imgs/8.png",
		text: "客服/反馈"
	}]

}])
app.service("sc",[function(){
		var obj={};
		obj.data={};
		obj.addGs=function(i){
			obj.data[i.id]=i;

		}
		obj.delGs=function(i){
			delete obj.data[i.id];
		}
		obj.moneys=0;
		obj.money=function(i){
			if(obj.moneys!=0){
				obj.moneys=0;
			}
			for(var a in i){
				obj.moneys+=(i[a].num*i[a].price);
			}
			return obj.moneys;
		}
		obj.selectAll=function(state){
			for(var i in obj.data){
				obj.data[i].state=state;
			}
		}
		obj.selectFn=function(state){
			state=true;
			for(var i in obj.data){
				if(!obj.data[i].state){
					state=false;
				};
			}
			return state;
		}
		return obj;
}])